Ext.define('Cps.view.dashboard.popup.Search1', {
	extend: 'Ext.window.Window',
	alias: 'widget.Search1',
	// Security Window
	height: 500,
  width: 800,
	title: Cps.Locale.Search.title2,
	modal: true,
	closable: true,
	//resizable: false,
	layout: {type: 'vbox', align: 'stretch'},
	items: [
		{
			xtype: 'form',
			bodyPadding: '7px 20px',
			layout: 'hbox',
			items: [
				{
					xtype: 'textfield',
					id: 'txtsearchTag1',
					width: 466,
					//flex: 3,
					labelWidth: 70,
					margin: '0 4 0 0',
					fieldLabel: Cps.Locale.Search.field,
					listeners: {
						specialkey: function(field, e){
						    if (e.getKey() == e.ENTER) {
								var gridSecurity = Ext.getCmp('gridSecurity');
								Ext.Ajax.request({
									method: 'GET',
									url: Cps.Config.Api.getSecurity,
									params: getParam,
									success: function(data) {
										if (IsJson(data.responseText))
										{
											data = Ext.JSON.decode(data.responseText).data;
											data = data ? data.ttSecurity : null;
											data = data ? data.ttSecurityRow : null;
											var f_data = [];
											if (data != undefined)
											{
												if (data.length != undefined)
												{
													for(i = 0; i < data.length; i++) {
														f_data.push(data[i]);
													}
												}
												else
													f_data.push(data);
											}
											gridSecurity.store.loadData(f_data);
											gridSecurity.getView().focusRow(0);
										}
									},
									scope: this
								});
              }
            }
        	}
				},
				{
					xtype: 'button',
					width: 120,
					//flex: 1,
					text: Cps.Locale.Search.btn1,
					handler: function() {
						var gridSecurity = Ext.getCmp('gridSecurity');
						Ext.Ajax.request({
							method: 'GET',
							url: Cps.Config.Api.getSecurity,
							params: getParam,
							success: function(data) {
								if (IsJson(data.responseText))
								{
									data = Ext.JSON.decode(data.responseText).data;
									data = data ? data.ttSecurity : null;
									data = data ? data.ttSecurityRow : null;
									var f_data = [];
									if (data != undefined)
									{
										if (data.length != undefined)
										{
											for(i = 0; i < data.length; i++) {
												f_data.push(data[i]);
											}
										}
										else
											f_data.push(data);
									}
									gridSecurity.store.loadData(f_data);
									gridSecurity.getView().focusRow(0);
								}
							},
							scope: this
						});
					}
				}
			]
		},
		{
			xtype: 'grid',
			id: 'gridSecurity',
			flex: 2,
			store: Ext.create('Ext.data.Store', {
				fields: [
					{ name: 'SecurityId', type: 'int' },
					{ name: 'SecurityCode', type: 'string' },
					{ name: 'SecurityLabel', type: 'string' },
					{ name: 'SecurityCurrency', type: 'string' },
					{ name: 'SecurityClass', type: 'string' }
				],
				data: []
			}),
			columns: {
				items: [
					{
						text: Cps.Locale.Search.column1,
						dataIndex: 'SecurityCode',
						flex: 2,
						hideable: false
					},
					{
						text: Cps.Locale.Search.column2,
						dataIndex: 'SecurityLabel',
						flex: 5,
						hideable: false
					},
					{
						flex: 1,
						text: Cps.Locale.Search.column3,
						dataIndex: 'SecurityCurrency',
						hideable: false
					},
					{
						text: Cps.Locale.Search.column4,
						dataIndex: 'SecurityClass',
						hideable: false,
						flex: 2
					}
				]
			},
			listeners: {
				itemdblclick: function( grid, record, item, index, e, eOpts ){
					returnData1();	
				}
			}
		}
	],
	buttons: [
		{
			text: Cps.Locale.Search.btn2,
			handler: function() {
				returnData1();
			}
		},
		{
			text: Cps.Locale.Search.btn3,
			handler: function() {
				this.ownerCt.ownerCt.close();
			}
		}
	],
	listeners:{
    close:function( panel, eOpts ){
    	var grid= panel.down('grid');
			var store= grid.getStore();
			store.removeAll();
    }
  }
});

function returnData1(){
	var txtSecurityCode = Ext.getCmp('txtSecurityCode'),
		txtSecurityLabel = Ext.getCmp('txtSecurityLabel'),
		hdfSecurityId = Ext.getCmp('hdfSecurityId'),
		gridSecurity = Ext.getCmp('gridSecurity'), 
		select;
	if (gridSecurity.getSelectionModel().getSelection() != "")
	{
		select = gridSecurity.getSelectionModel().getSelection();
		txtSecurityCode.setValue(select[0].data.SecurityCode);
		txtSecurityLabel.setValue(select[0].data.SecurityLabel);
		hdfSecurityId.setValue(select[0].data.SecurityId);
	}
	var win= gridSecurity.up('window');
	win.close();
}
